package com.ttdevs.es;

import android.app.ActionBar;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TabHost;
import android.widget.TextView;

import com.ttdevs.util.StaticConstants;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements StaticConstants {

	private final static int TAB_INDEX_SETTINGS = 0; // 设置
	private final static int TAB_INDEX_START = 1; // main
	private final static int TAB_INDEX_PREF = 2; // 启动参数

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		TabHost tabHost = getTabHost();
		tabHost.addTab(tabHost.newTabSpec(TAB_SETTINGS).setIndicator(TAB_SETTINGS)
				.setContent(new Intent(this, SettingsActivity.class)));
		tabHost.addTab(tabHost.newTabSpec(TAB_MAIN).setIndicator(TAB_MAIN)
				.setContent(new Intent(this, StartActivity.class)));
		tabHost.addTab(tabHost.newTabSpec(TAB_PREFERENCE).setIndicator(TAB_PREFERENCE)
				.setContent(new Intent(this, PreferenceActivity.class)));
		tabHost.setCurrentTab(TAB_INDEX_START);

		getTabWidget().setVisibility(View.GONE);
		initActionBar();
	}

	private TextView tvTitle;

	private void initActionBar() {
		ActionBar bar = getActionBar();
		View mCustomView = getLayoutInflater().inflate(R.layout.actionbar_main, null);
		bar.setCustomView(mCustomView, new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		int flags = 0;
		flags = ActionBar.DISPLAY_HOME_AS_UP;
		flags ^= ActionBar.DISPLAY_SHOW_HOME;
		flags ^= ActionBar.DISPLAY_USE_LOGO;
		flags ^= ActionBar.DISPLAY_SHOW_TITLE;
		flags ^= ActionBar.DISPLAY_SHOW_CUSTOM;
		int change = bar.getDisplayOptions() ^ flags;
		bar.setDisplayOptions(change, flags);

		// mCustomView.setBackgroundColor(Color.GRAY);
		tvTitle = (TextView) mCustomView.findViewById(R.id.tvTitle);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		for (int i = 0; i < menu.size(); i++) {
			refreshTabIcon(menu.getItem(i), getTabHost().getCurrentTab());
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getGroupId() == R.id.tab_group) {
			switch (item.getItemId()) {
			case R.id.tab_menu_settings:
				getTabHost().setCurrentTab(TAB_INDEX_SETTINGS);
				tvTitle.setText("启动选项");
				break;

			case R.id.tab_menu_preference:
				getTabHost().setCurrentTab(TAB_INDEX_PREF);
				tvTitle.setText("系统设置");
				break;

			case R.id.tab_menu_start:
				getTabHost().setCurrentTab(TAB_INDEX_START);
				tvTitle.setText("T-T轻松防盗");
				break;

			default:
				break;
			}
			invalidateOptionsMenu();
		}
		return super.onOptionsItemSelected(item);
	}

	private void refreshTabIcon(MenuItem item, int currentTab) {
		if (item.getGroupId() == R.id.tab_group) {
			switch (item.getItemId()) {
			case R.id.tab_menu_preference:
				item.setIcon(currentTab == TAB_INDEX_PREF ? R.drawable.ic_tab_preference_selected
						: R.drawable.ic_tab_preference_unselected);
				break;
			case R.id.tab_menu_start:
				item.setIcon(currentTab == TAB_INDEX_START ? R.drawable.ic_tab_start_selected
						: R.drawable.ic_tab_start_unselected);
				break;
			case R.id.tab_menu_settings:
				item.setIcon(currentTab == TAB_INDEX_SETTINGS ? R.drawable.ic_tab_settings_selected
						: R.drawable.ic_tab_settings_unselected);
				break;

			default:
				break;
			}
		}
	}
}
